import 'dart:io';
import 'dart:typed_data';
import 'dart:ui';

import 'package:flutter/material.dart';

import '../../widgets/image/image_holder.dart';
import 'image_util.dart';

mixin ImageConvert {
  static const String _ossProcessOrder = '?x-oss-process=image';

  static ImageProvider getAssetImage(String name, {String? package}) {
    return AssetImage(name, package: package);
  }

  /// 图片写入File
  static Future<String> getImageFilePath(dynamic image, String filePath) async {
    final ByteData byteData = await image.toByteData(format: ImageByteFormat.png);
    final Uint8List pngBytes = byteData.buffer.asUint8List();
    final File file = File('$filePath/${DateTime.now().millisecondsSinceEpoch}.png');
    if (!file.existsSync()) {
      file.createSync();
    }
    file.writeAsBytesSync(pngBytes);
    return file.path;
  }

  /// oss 图片格式转换命令
  static String _ossFormatOrder(String imgExtensions) {
    assert(imgExtensions.isNotEmpty, 'Image file extensions can not empty !');
    return '/format,$imgExtensions';
  }

  /// 格式化图片URL
  static String formatImgURL(String imgUrl, ScaleType scaleType, {int scaleRatio = 100, int? targetWidth = 1440}) {
    switch (scaleType) {
      case ScaleType.scaleByWidth:
        return formatAndKeepRatioScaleByWidth2Webp(imgUrl, targetWidth: targetWidth);
      case ScaleType.scaleByRatio:
        return formatAndKeepRatioScaleImg2Webp(imgUrl, scaleRatio: scaleRatio);
      default:
        return imgUrl;
    }
  }

  /// 缩小目标比原图大，将使用原图
  static String formatAndKeepRatioScaleByWidth2Webp(String imgUrl, {int? targetWidth = 2000}) {
    if (imgUrl.contains('x-oss-process')) {
      return imgUrl;
    }
    assert(targetWidth! > 0, 'scale ratio must positive !');
    final String formatOrder = _ossFormatOrder('webp');
    String targetImgUrl;
    if (imgUrl.contains(_ossProcessOrder)) {
      // 说明原始链接已经拼接了一些oss指令
      targetImgUrl = '$imgUrl$formatOrder';
    } else {
      targetImgUrl = '$imgUrl$_ossProcessOrder/resize,w_$targetWidth,m_lfit,limit_1$formatOrder';
    }
    return targetImgUrl;
  }

  /// 将图片转为webp格式，并保持原图比例进行缩放
  /// * [scaleRatio] < 100 为缩小， >100 为放大
  static String formatAndKeepRatioScaleImg2Webp(String imgUrl, {int scaleRatio = 100}) {
    if (imgUrl.contains('x-oss-process')) {
      return imgUrl;
    }

    assert(scaleRatio > 0, 'scale ratio must positive !');
    final String formatOrder = _ossFormatOrder('webp');
    String targetImgUrl;
    if (imgUrl.contains(_ossProcessOrder)) {
      // 说明原始链接已经拼接了一些oss 指令
      targetImgUrl = '$imgUrl$formatOrder';
    } else {
      targetImgUrl = '$imgUrl$_ossProcessOrder/resize,p_$scaleRatio$formatOrder';
    }
    return targetImgUrl;
  }

  /// 通用图片加载中占位图
  static Widget retrieveCommonPlaceholder() {
    return const ImageLoadingWidget();
  }

  /// 通用图片加载错误占位图
  static Widget retrieveCommonImgErrorWidget({
    Key? key,
    double? width,
    double? height,
  }) {
    return ImageLoadErrorWidget(key: key, width: width, height: height);
  }
}
